// pages/chooseSeat/chooseSeat.js

Page({

  /**
   * 页面的初始数据
   */
  data: {
    appointRecord:[],
    array:[],
    number:1,
    title:'', //预约日期
    startTime:'',
    endTime: '',
    username: '',
    userwxid: '',
    checked: false,
    countchoose:0, //用户预约个数
  },
  appointsuccess(){
    wx.showToast({
      title: '预约成功',
      icon: 'success',
      duration: 2000
    });
    
  },
  changes(e){
    this.setData({
      number:e.detail
    });
  },
  onChange(event) {
    this.setData({
      checked: event.detail,
    });
  },

  //调用导航服务
  invokeNavigation(){
    wx.openLocation({
      latitude: 30.463889,  // 30°27'50"N
      longitude: 114.436111,  // 114°26'10"E
      scale: 18,
      name: '你要去的地方',
      address: '武汉工程大学流芳校区计算机科学与技术学院、人工智能学院'
    })
    
  },
  getArray(){
  
  
    wx.request({
      url: 'http://8.219.238.141/user/getUserByIdentity?identity=0',
      method: 'POST',
      success: (res) => {
        console.log(res)
        this.setData({
          array: res.data.data
        })
  
        
      }
    });
  
  },
  commitAppoint(){

// 获取当前年份
var year = new Date().getFullYear();

// 创建一个新的日期字符串
var yearMonthDay = year + "-" + this.data.title;

// 将日期字符串转换为日期对象
var appointdate = new Date(yearMonthDay);
// 获取年份、月份和日期
var year1 = appointdate.getFullYear();
var month1 = appointdate.getMonth() + 1;  // JavaScript中，月份是从0开始的，所以需要加1
var day1 = appointdate.getDate();

// 补零
month1 = month1< 10 ? '0' + month1 : month1;
day1 = day1 < 10 ? '0' + day1 : day1;

// 拼接字符串
var formattedDate = year1 + '-' + month1 + '-' + day1;
console.log(formattedDate);
    // 获取当前日期和时间e
  var date = new Date();

  // 格式化日期和时间
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var day = date.getDate();
  var hour = date.getHours();
  var minute = date.getMinutes();
  var second = date.getSeconds();

  // 补零
  month = month < 10 ? '0' + month : month;
  day = day < 10 ? '0' + day : day;
  hour = hour < 10 ? '0' + hour : hour;
  minute = minute < 10 ? '0' + minute : minute;
  second = second < 10 ? '0' + second : second;

// 拼接字符串
var createtime = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;


// 假设你有一个只包含时间的字符串

// 获取当前日期
var date2 = new Date();

// 格式化日期
var year2 = date.getFullYear();
var month2 = date.getMonth() + 1;  // JavaScript中，月份是从0开始的，所以需要加1
var day2 = date.getDate();

// 补零
month2 = month2 < 10 ? '0' + month2 : month2;
day2 = day2 < 10 ? '0' + day2: day2;

// 拼接字符串
var start = year2 + '-' + this.data.title + ' ' + this.data.startTime + ':00';
var end = year2 + '-' + this.data.title + ' ' + this.data.endTime + ':00';
    console.log(appointdate);
    var appointMent = {appointDate:formattedDate,wxid: this.data.userwxid,seatNumber:this.data.number,startTime:start,endTime:end,createTime:createtime,isUsed:false};
    console.log(this.data.startTime);
    console.log(this.data.endTime);
    console.log(createtime);
    let user = this.data.array.find(user => user.wxid === this.data.userwxid);
    let sameRecord1 = this.data.appointRecord.some(record => 
      record.wxid === this.data.userwxid 
    );
    let sameRecord2 = this.data.appointRecord.some(record => 
      record.startTime === start 
    );
    let sameRecord3 = this.data.appointRecord.some(record => 
      record.endTime === end 
    );
    // 设置布尔变量，如果找到对象则为 true，否则为 false
    let isAppointmentFound = sameRecord1&&sameRecord2&&sameRecord3;
    console.log(isAppointmentFound);
    console.log(sameRecord1);
    console.log(sameRecord2);
    console.log(sameRecord3);
      if(user.status === true && user.name != null &&  isAppointmentFound === false){
      wx.request({      
        url: 'http://8.219.238.141/appointment/addAppointment',
        method: 'POST',
        data: appointMent,
        success: function(res) {
          console.log(res);
          if (res.data.msg === 'success') {
            wx.showToast({
              title: '预约成功',
              icon: 'success'
            });
            
          }; 
        }
      });
    }
    else if(user.status === true && user.name != null &&  isAppointmentFound === true){
      wx.showToast({
        title: '不能重复预约',
        icon:'error'
      })
    }
    else if (user.status === false){
      wx.showToast({
        title: '您的账号已被禁用',
        icon:'error'
      })
    }
    else if(user.name === null){
      wx.showToast({
        title: '请先实名认证',
        icon: 'error'
      })
    }
    

  },
  getAppointRecord(){
    wx.request({
      url: `http://8.219.238.141/appointment/selectByWxid?wxid=${this.data.userwxid}`,
      method:'GET',
      success: (res) => {
        console.log(res);
        this.setData({
          appointRecord: [...this.data.appointRecord, ...res.data.data]
        });
    }
  });
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
    this.getArray();
    this.setData({
      startTime: options.startTime,
      endTime:options.endTime,
      title:options.title
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var app = getApp();
    this.setData({
      username: app.globalData.userName,
      userwxid: app.globalData.userWxid,
    });
    this.getAppointRecord();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})